/**
 * 神州信息(2012)
 */
package net.abbp.watchkeeper;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.abbp.dao.DaoFactory;
import net.abbp.dao.IDao;
import net.abbp.dao.condition.Cnds;
import net.abbp.dao.condition.Operator;

/**
 * 值班人员管理
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        Date：2012-2-4
 */
public class WatchKeeperEditor extends JPanel {
    private JComboBox cbx1;
    private JComboBox cbx2;
    private JComboBox cbx3;
    private JComboBox cbx4;
    private JComboBox cbx5;
    private JComboBox cbx6;
    private JComboBox cbx7;
    private static final Insets COMP_INSETS = new Insets(4, 4, 4, 4);
    IDao dao = DaoFactory.INSTANCE.newDao();

    public WatchKeeperEditor() {
        this.setLayout(new BorderLayout());
        this.add(new JLabel("值班人员设置", JLabel.CENTER), BorderLayout.NORTH);
        Object[] names = this.getNames().toArray();

        JPanel paneInput = new JPanel(new GridBagLayout());
        paneInput.setBorder(BorderFactory.createEtchedBorder());
        paneInput.add(new JLabel("星期一"), newGridBagConstraints(0, 0, 1, 1));
        this.cbx1 = new JComboBox(names);
        this.cbx1.setPreferredSize(new Dimension(200, 24));
        paneInput.add(this.cbx1, newGridBagConstraints(1, 0, 1, 1));

        paneInput.add(new JLabel("星期二"), newGridBagConstraints(0, 1, 1, 1));
        this.cbx2 = new JComboBox(names);
        paneInput.add(this.cbx2, newGridBagConstraints(1, 1, 1, 1));

        paneInput.add(new JLabel("星期三"), newGridBagConstraints(0, 2, 1, 1));
        this.cbx3 = new JComboBox(names);
        paneInput.add(this.cbx3, newGridBagConstraints(1, 2, 1, 1));

        paneInput.add(new JLabel("星期四"), newGridBagConstraints(0, 3, 1, 1));
        this.cbx4 = new JComboBox(names);
        paneInput.add(this.cbx4, newGridBagConstraints(1, 3, 1, 1));

        paneInput.add(new JLabel("星期五"), newGridBagConstraints(0, 4, 1, 1));
        this.cbx5 = new JComboBox(names);
        paneInput.add(this.cbx5, newGridBagConstraints(1, 4, 1, 1));

        paneInput.add(new JLabel("星期六"), newGridBagConstraints(0, 5, 1, 1));
        this.cbx6 = new JComboBox(names);
        paneInput.add(this.cbx6, newGridBagConstraints(1, 5, 1, 1));

        paneInput.add(new JLabel("星期日"), newGridBagConstraints(0, 6, 1, 1));
        this.cbx7 = new JComboBox(names);
        paneInput.add(this.cbx7, newGridBagConstraints(1, 6, 1, 1));

        JButton btnOK = new JButton("确定");
        btnOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                WatchkeeperInfo wi = new WatchkeeperInfo();

                wi.setF1(String.valueOf(cbx1.getSelectedItem()));
                wi.setF2(String.valueOf(cbx2.getSelectedItem()));
                wi.setF3(String.valueOf(cbx3.getSelectedItem()));
                wi.setF4(String.valueOf(cbx4.getSelectedItem()));
                wi.setF5(String.valueOf(cbx5.getSelectedItem()));
                wi.setF6(String.valueOf(cbx6.getSelectedItem()));
                wi.setF7(String.valueOf(cbx7.getSelectedItem()));
                wi.setId(1);

                dao.update(wi);
            }
        });
        paneInput.add(btnOK, newGridBagConstraints(0, 7, 2, 1));

        this.add(paneInput);

        this.upateUIThroughModel();
    }

    /**
     * 
     */
    private void upateUIThroughModel() {
        WatchkeeperInfo wi = dao.fetch(WatchkeeperInfo.class);

        this.cbx1.setSelectedItem(wi.getF1());
        this.cbx2.setSelectedItem(wi.getF2());
        this.cbx3.setSelectedItem(wi.getF3());
        this.cbx4.setSelectedItem(wi.getF4());
        this.cbx5.setSelectedItem(wi.getF5());
        this.cbx6.setSelectedItem(wi.getF6());
        this.cbx7.setSelectedItem(wi.getF7());
    }

    private List<String> getNames() {
        List<Linkman> list = dao.query(Linkman.class, Cnds.and("receive", Operator.NotEqual, "接收短信"));

        List<String> vs = new ArrayList<String>();
        vs.add("");

        for (Linkman man : list) {
            vs.add(man.getName());
        }

        return vs;
    }

    private GridBagConstraints newGridBagConstraints(int gridX, int gridY, int gridWidth, int gridHeight) {
        return new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, COMP_INSETS, 0, 0);
    }
}
